package org.wanxian.stores.db;

import java.util.ArrayList;

import org.wanxian.stores.model.Stores;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class StoresDao {
	private DatabaseOpenHelper dbHelper;
	private static final String TABLE_NAME = "stores";
	
	public StoresDao(Context context) {
		dbHelper = new DatabaseOpenHelper(context);
	}
	
	public boolean isExist(Stores stores) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "id = ?", new String[] {String.valueOf(stores.getId())}, null, null, null);
		if(cursor.getCount() > 0) {
			cursor.close();
			db.close();
			return true;
		}
		return false;
	}
	
	public int insert(Stores stores) {
		Stores bef = queryById(stores.getId());
		if(bef != null) {
			deleteById(bef.getId());
		}
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put("userId", stores.getUserId());
		value.put("name", stores.getName());
		value.put("phone", stores.getPhone());
		value.put("core", stores.getCore());
		value.put("address", stores.getAddress());
		value.put("image", stores.getImage());
		value.put("distance", stores.getDistance());
		value.put("longitude", stores.getLongitude());
		value.put("latitude", stores.getLatitude());
		int id = (int) db.insert(TABLE_NAME, null, value);
		return id;
	}

	public int update(Stores stores) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put("userId", stores.getUserId());
		value.put("core", stores.getCore());
		value.put("name", stores.getName());
		value.put("address", stores.getAddress());
		value.put("image", stores.getImage());
		value.put("distance", stores.getDistance());
		value.put("longitude", stores.getLongitude());
		value.put("latitude", stores.getLatitude());
		int index = (int) db.update(TABLE_NAME, value, "id = ?", new String[]{String.valueOf(stores.getId())});
		db.close();
		return index;
	}
	
	public void deleteById(int id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE_NAME, "id = ?", new String[] {String.valueOf(id)});
		db.close();
	}
	
	public Stores queryByUserId(int userId) {
		Stores stores = null;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "userId = ?", new String[]{String.valueOf(userId)}, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			String address = cursor.getString(cursor.getColumnIndex("address"));
			String image = cursor.getString(cursor.getColumnIndex("image"));
			int core = cursor.getInt(cursor.getColumnIndex("core"));
			int distance = cursor.getInt(cursor.getColumnIndex("distance"));
			double longitude = cursor.getDouble(cursor.getColumnIndex("longitude"));
			double latitude = cursor.getDouble(cursor.getColumnIndex("latitude"));
			stores = new Stores(id, userId,image, name, phone, core, address, distance, longitude, latitude);
		}
		cursor.close();
		db.close();
		return stores;
	}

	
	public Stores queryById(int id) {
		Stores stores = null;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, "id = ?", new String[]{String.valueOf(id)}, null, null, null);
		while(cursor.moveToNext()) {
			int userId = cursor.getInt(cursor.getColumnIndex("userId"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			String address = cursor.getString(cursor.getColumnIndex("address"));
			String image = cursor.getString(cursor.getColumnIndex("image"));
			int core = cursor.getInt(cursor.getColumnIndex("core"));
			int distance = cursor.getInt(cursor.getColumnIndex("distance"));
			double longitude = cursor.getDouble(cursor.getColumnIndex("longitude"));
			double latitude = cursor.getDouble(cursor.getColumnIndex("latitude"));
			stores = new Stores(id, userId,image, name, phone, core, address, distance, longitude, latitude);
		}
		cursor.close();
		db.close();
		return stores;
	}

	
	public ArrayList<Stores> queryAll() {
		ArrayList<Stores> list = new ArrayList<Stores>();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		while(cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			int userId = cursor.getInt(cursor.getColumnIndex("userId"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String phone = cursor.getString(cursor.getColumnIndex("phone"));
			int core = cursor.getInt(cursor.getColumnIndex("core"));
			String image = cursor.getString(cursor.getColumnIndex("image"));
			String address = cursor.getString(cursor.getColumnIndex("address"));
			int distance = cursor.getInt(cursor.getColumnIndex("distance"));
			double longitude = cursor.getDouble(cursor.getColumnIndex("longitude"));
			double latitude = cursor.getDouble(cursor.getColumnIndex("latitude"));
			Stores stores = new Stores(id, userId,image, name, phone, core, address, distance, longitude, latitude);
			list.add(stores);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	public int getCount() {
		return queryAll().size();
	}
}
